package com.chinairi.mall.controller;


import com.chinairi.common.annotation.DoPage;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.common.utils.UserSession;
import com.chinairi.mall.controller.viewobject.*;
import com.chinairi.mall.controller.viewobject.OrderInfoVO;
import com.chinairi.mall.model.CancelOrder;
import com.chinairi.mall.model.OrderInfo;
import com.chinairi.mall.service.ICancelOrderService;
import com.chinairi.mall.service.IGoodsShoppingCartService;
import com.chinairi.mall.service.IOrderInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单表,由于order是MYSQL的关键字，所以换名 前端控制器
 * </p>
 *
 * @author gsl
 * @since 2020-09-30
 */
@RestController
@RequestMapping("/order-info")
public class OrderInfoController extends BaseController {

    @Autowired
    private IOrderInfoService iOrderInfoService;

    @Autowired
    private ICancelOrderService cancelOrderService;

    @Autowired
    private IGoodsShoppingCartService iGoodsShoppingCartService;

    @GetMapping("/refuseOrder")
    public void refuseOrder(String orderId) throws IOException {
        iOrderInfoService.refuseOrder(orderId);
        ResponseUtil.showMessage("success", "订单拒绝成功", "");
    }

    /**
     * 生成订单，包括实物和服务订单
     *
     * @param orderInfoVOList
     * @throws IOException
     */
    @PostMapping("/addOrder")
    public void addOrder(@RequestBody List<OrderInfoVO> orderInfoVOList) throws IOException {
        List<OrderInfoVO> orderResultList = new ArrayList<>(orderInfoVOList.size());
        for (OrderInfoVO orderInfoVO : orderInfoVOList) {
            OrderInfoVO orderResult = iOrderInfoService.addOrder(orderInfoVO);
            orderResultList.add(orderResult);
        }
        ResponseUtil.showMessage("success", "订单生成成功,请确认订单！", orderResultList);
    }

    /**
     * 订单确认
     *
     * @param orderVO
     * @throws IOException
     */
    @PostMapping("/confirmOrder")
    public void confirmOrder(@RequestBody OrderVO orderVO) throws Exception {
        for (OrderInfoVO orderInfoVO : orderVO.getOrderInfoVOList()) {
            iOrderInfoService.confirmOrder(orderInfoVO, orderVO.getScIdList());
        }
        ResponseUtil.showMessage("success", "下单成功，订单已确认", "");
    }

    /**
     * 修改订单状态
     *
     * @param orderId
     * @param
     * @throws IOException
     */
    @PutMapping("/updateOrderState")
    public void updateOrderState(String orderId, String orderState) throws IOException {
        iOrderInfoService.updateOrderState(orderId, orderState);
        ResponseUtil.showMessage("success", "订单状态修改成功", "");
    }

    /**
     * 获取用户的订单列表
     *
     * @param orderInfo
     * @throws IOException
     */
    @GetMapping("/getOrder")
    public void getOrder(OrderInfo orderInfo) throws IOException {
        List<OrderInfo> orderInfoList = iOrderInfoService.getOrder(orderInfo);
        ResponseUtil.showMessage("success", "获取订单成功", orderInfoList);
    }

    /**
     * 根据订单编号获取订单下商品详情（包括实物和服务）
     *
     * @param orderId
     * @throws IOException
     */
    @GetMapping("/getOrderDetail")
    public void getOrderDetail(String orderId) throws IOException {
        OrderInfoVO orderInfoVO = iOrderInfoService.getOrderDetail(orderId);
        ResponseUtil.showMessage("success", "获取订单详情成功", orderInfoVO);
    }

    /**
     * 获取店铺订单列表
     *
     * @param orderInfoVO
     * @throws IOException
     */
    @GetMapping("/getShopOrderList")
    public void getShopOrder(OrderInfoVO orderInfoVO) throws IOException {
        List<OrderInfo> orderInfoList = iOrderInfoService.getShopOrderList(orderInfoVO);
        ResponseUtil.showMessage("success", "获取商品店铺订单成功", orderInfoList);
    }

    /**
     * 查询订单时效
     *
     * @param proProcessParam
     * @return
     */
    @GetMapping("/getProProcessInfo")
    @DoPage
    public List<Map<String, Object>> getProProcessInfo(ProProcessParam proProcessParam) throws ParseException {
        return iOrderInfoService.getProProcessInfo(proProcessParam);
    }

    /**
     * 导出订单时效信息
     *
     * @param response
     * @param proProcessParam 订单时效条件查询参数
     * @throws IOException
     */
    @GetMapping("/exportOrderInfo")
    public void exportOrderInfo(HttpServletResponse response, ProProcessParam proProcessParam) throws IOException, ParseException {
        iOrderInfoService.exportOrderInfo(response, proProcessParam);
    }

    /**
     * 获取视频的表头信息
     *
     * @param saleOrderNo
     * @return
     */
    @GetMapping("/getAllTechnologyName")
    public List<Map<String, Object>> getAllTechnologyName(String saleOrderNo, String matId) {
        return iOrderInfoService.getAllTechnologyName(saleOrderNo, matId);
    }

    /**
     * 获取视频信息
     *
     * @return
     */
    @GetMapping("/getTeVideo")
    public List<Map<String, Object>> getTeVideo() {
        return iOrderInfoService.getTeVideo();
    }

    /**
     * 获取订单进度详情
     *
     * @param productProduceProcessId 生产进度id
     * @return
     */
    @GetMapping("/getPurOrderDetail")
    public List<Map<String, Object>> getPurOrderDetail(String productProduceProcessId) {
        return iOrderInfoService.getPurOrderDetail(productProduceProcessId);
    }

    /**
     * 导出急料表
     *
     * @param response
     * @param productProduceProcessId 生产进度id
     * @throws IOException
     */
    @GetMapping("/exportOrderDetail")
    public void exportOrderDetail(HttpServletResponse response, String productProduceProcessId) throws IOException {
        iOrderInfoService.exportOrderDetail(response, productProduceProcessId);
    }

    /**
     * 质量数据查询
     *
     * @param productQltReportParam
     * @return
     */
    @GetMapping("/getProductQltReportList")
    @DoPage
    public List<Map<String, Object>> getProductQltReportList(ProductQltReportParam productQltReportParam) throws ParseException {
        return iOrderInfoService.getProductQltReportList(productQltReportParam);
    }

    /**
     * 质量数据信息导出
     *
     * @param response
     * @param productQltReportParam
     * @throws IOException
     * @throws ParseException
     */
    @GetMapping("/exportProductQltReport")
    public void exportProductQltReport(HttpServletResponse response, ProductQltReportParam productQltReportParam) throws IOException, ParseException {
        iOrderInfoService.exportProductQltReport(response, productQltReportParam);
    }

    /**
     * 成品检验数据查询
     *
     * @param isVideo
     * @return
     */
    @GetMapping("/getMatTechnologyQltReportList")
    @DoPage
    public List<Map<String, Object>> getMatTechnologyQltReportList(String qltId, String isVideo) {
        return iOrderInfoService.getMatTechnologyQltReportList(qltId, isVideo);
    }

    /**
     * 成品质量数据导出
     *
     * @param response
     * @param pqId
     * @param isVideo
     * @throws IOException
     * @throws ParseException
     */
    @GetMapping("/exportMatTechnologyQltReport")
    public void exportMatTechnologyQltReport(HttpServletResponse response, String pqId, String isVideo) throws IOException, ParseException {
        iOrderInfoService.exportMatTechnologyQltReport(response, pqId, isVideo);
    }

    /**
     * 工序检验数据查询
     *
     * @return
     */
    @GetMapping("/getTechnologyReportList")
    @DoPage
    public List<Map<String, Object>> getTechnologyReportList(TechnologyReportParam technologyReportParam) {
        return iOrderInfoService.getTechnologyReportList(technologyReportParam);
    }

    /**
     * 工序检验数据导出
     *
     * @param response
     * @param technologyReportParam
     * @throws IOException
     * @throws ParseException
     */
    @GetMapping("/exportTechnologyReport")
    public void exportTechnologyReport(HttpServletResponse response, TechnologyReportParam technologyReportParam) throws IOException, ParseException {
        iOrderInfoService.exportTechnologyReport(response, technologyReportParam);
    }

    /**
     * 获取检验报告地址
     *
     * @param mqId 质检明细表id
     * @return
     */
    @GetMapping("/getTechnologyFile")
    public String getTechnologyFile(String mqId) {
        return iOrderInfoService.getTechnologyFile(mqId);
    }

    /**
     * 框架协议库存查询
     *
     * @param stockReportParam
     * @return
     */
    @GetMapping("/getStockReportList")
    @DoPage
    public List<Map<String, Object>> getStockReportList(StockReportParam stockReportParam) {
        return iOrderInfoService.getStockReportList(stockReportParam);
    }

    /**
     * 导出框架协议库存信息
     *
     * @param response
     * @param stockReportParam
     * @throws IOException
     */
    @GetMapping("/exportStockReport")
    public void exportStockReport(HttpServletResponse response, StockReportParam stockReportParam) throws IOException {
        iOrderInfoService.exportStockReport(response, stockReportParam);
    }

    /**
     * 获取质量数据类型下拉
     *
     * @return
     */
    @GetMapping("/listDataType")
    public List<Map<String, Object>> listDataType() {
        return iOrderInfoService.listDataType();
    }

    /**
     * 获取买家用户所有实物订单和服务订单的状态
     *
     * @param createUser
     * @throws IOException
     */
    @GetMapping("/getOrderSate")
    public void getOrderSate(String createUser) throws IOException {
        ResponseUtil.showMessage("success", "获取成功", iOrderInfoService.getOrderSate(createUser));
    }

    /**
     * 获取买家用户的订单大概信息
     *
     * @throws IOException
     */
    @GetMapping("/getOrderByUserId")
    public void getOrderByUserId(String orderId) throws IOException {
        ResponseUtil.showMessage("success", "获取成功", iOrderInfoService.getOrderByUserId(orderId));
    }

    /**
     * 重新生成服务订单
     *
     * @throws IOException
     */
    @PostMapping("/reAddOrderService")
    public void reAddOrderService(@RequestBody OrderServiceVO orderServiceVO) throws IOException {
        iOrderInfoService.reAddOrderService(orderServiceVO);
        ResponseUtil.showMessage("success", "获取成功", "");
    }

    @PostMapping("sendToProvider")
    public Map<String, Object> sendToProvider(@RequestBody OrderInfoVO orderInfoVO) throws Exception {
        return iOrderInfoService.sendToProvider(orderInfoVO);
    }

    @PostMapping("orderStatus")
    public Map<String, Object> updateOrderStatus(@RequestBody Map<String, Object> map) {
        return iOrderInfoService.updateOrderStatus(map);
    }

    /**
     * 获取当前用户下的商品订单统计
     * @return
     */
    @GetMapping("/orderInfoByUser")
    public List<Map<String,Object>> getOrderInfoByUserId(){
        return iOrderInfoService.getOrderInfoByUserId(UserSession.getUserId());
    }

    /**
     * 获取当前用户下的服务订单统计
     * @param shopType
     * @return
     */
    @GetMapping("/serviceOrderInfoByUser")
    public List<Map<String,Object>> getServiceOrderInfoByUser(String shopType){
        return iOrderInfoService.getServiceOrderInfoByUser(UserSession.getUserId(),shopType);
    }

    /**
     * 新增撤销订单
     * @param cancelOrder 撤销订单实体
     * @throws IOException e
     */
    @PostMapping("/addCancelOrder")
    @ApiOperation("撤销订单")
    public void addCancelOrder(@RequestBody CancelOrder cancelOrder) throws IOException {
        cancelOrderService.addCancelOrder(cancelOrder);
        ResponseUtil.showMessage("success","请求撤销订单成功",null);
    }


    /**
     * 确认撤销订单
     * @param cancelOrder 撤销订单实体
     * @throws IOException e
     */
    @PostMapping("/confirmCancelOrder")
    @ApiOperation("确认撤销订单")
    public void confirmDeleteOrder(@RequestBody CancelOrder cancelOrder) throws IOException {
        cancelOrderService.confirmCancelOrder(cancelOrder);
        ResponseUtil.showMessage("success","撤销订单成功",null);
    }

    /**
     * 根据接收人id获取撤销订单相关提醒
     * @param receiverId 撤销订单接收人id
     * @throws IOException e
     */
    @GetMapping("getListRemindByReveiverId")
    @ApiOperation("根据接收人id获取撤销订单相关提醒")
    @ApiImplicitParam(name = "receiverId",value = "接收人id",required = true,paramType = "query")
    public void getListRemind(String receiverId) throws IOException {
        ResponseUtil.showMessage("success","请求成功",cancelOrderService.getRemindByReceiverId(receiverId));
    }

    /**
     * 根据接收人id查看所有待确认的撤销订单信息
     * @param receiverId 撤销订单接收人id
     * @throws IOException e
     */
    @GetMapping("getListPendingOrderByReceiverID")
    @ApiOperation("根据接收人id查看所有待确认的撤销订单信息")
    @ApiImplicitParam(name = "receiverId",value = "接收人id",required = true,paramType = "query")
    public void getListPendingOrderByReceiverID(String receiverId) throws IOException {
        ResponseUtil.showMessage("success","请求成功",cancelOrderService.getListPendingOrderByReceiverId(receiverId));
    }
}
